<template>
<div class="list-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 表单控件 -->
        <el-form  label-width="80px" ref="form" >
           <el-row type="flex" justify="space-between">
          <el-col :span="20">
           <el-form-item label="关键字" >
            <el-input v-model="query.keyword" placeholder="根据编号搜索" ></el-input>
            </el-form-item>
            </el-col>
          <el-col :span="4">  <el-form-item>
           <el-button size="mini" @click="del">清除</el-button>
           <el-button type="primary" size="mini" @click="onSubmit">搜索</el-button>
        </el-form-item></el-col>
         </el-row>
      </el-form>
         <!-- 数据条 -->
          <div class="itemCount" >
           <i class="el-icon-info"></i>
          <span>数据一共&emsp;{{total}}&emsp;条</span>
          </div>
          <!-- 表格控件 -->
     <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="编号" width="220">
      </el-table-column>
      <el-table-column prop="questionType" label="题型" width="100">
        <template slot-scope="scope">
          {{scope.row.questionType|questionTypes}}
        </template>
      </el-table-column>
      <el-table-column width="260px"  prop="questionIDs"  label="题目编号" >
      <template slot-scope="scope">
         <el-link type="primary" :underline="false">
           <div  v-for="(item,index) in scope.row.questionIDs"
          :key="index" @click="showPreview(item)">
           {{item.number}}></div>
         </el-link>
      </template>
      </el-table-column>
      <el-table-column prop="addTime" label="录入时间" width="180">
      </el-table-column>
      <el-table-column prop="totalSeconds" label="答题时间(s)" >
      </el-table-column>
      <el-table-column prop="accuracyRate" label="正确率(%)" >
      </el-table-column>
      <el-table-column prop="userName" label="录入人" >
      </el-table-column>
      <el-table-column label="操作" width="80">
        <template slot-scope="scope">
        <el-button type="danger" icon="el-icon-delete" class="elIcon" circle plain @click="remove(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
       <!-- 分页控件 -->
    <el-row type="flex"  justify="end">
       <el-col :push="12">
     <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.page"
      :page-sizes="[20, 30, 50, 100]"
      :page-size="query.pagesize"
      layout=" prev, pager, next,sizes, jumper"
      :total="total"
      >
    </el-pagination>
  </el-col>
</el-row>

 <questions-preview :data="showList" ref="question"/>

  </el-card>
   </div>
    </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '../../api/hmmm/questions'
import QuestionsPreview from '../components/questions-preview.vue'
export default {
  name: 'QusetionList',
  components: {
    QuestionsPreview
  },
  data () {
    return {
      tableData: [], // 渲染表格数据
      query: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      total: null, // 总条数
      showList: {} // 展示数据详情
    }
  },
  created () {
    // 获取题组列表
    this.randoms()
  },
  methods: {
    // 点击预览
    showPreview (value) {
      this.showList = value// 获取数据详情
      this.$nextTick(() => {
        this.$refs.question.open()
      })
    },
    // 获取数据详情
    async detailFn (data) {
      const res = await detail(data)
      this.showList = res.data
    },
    // 删除
    async remove (row) {
      const delForm = await this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (delForm === 'cancel') return this.$message('您已经取消')

      await removeRandoms(row)
      this.$message({
        type: 'success',
        message: '删除成功!'
      })

      if (this.tableData.length === 1) {
        this.query.page--
      }
      if (this.query.page < 1) {
        this.query.page = 1
      }

      // 获取题组列表
      this.randoms()
    },
    // 清除按钮，清空关键字输入框
    del () {
      this.query = {
        keyword: ''
      }
    },
    // 每页条数
    handleSizeChange (newSize) {
      this.query.pagesize = newSize
      // 题组列表
      this.randoms()
    },
    // 当前页
    handleCurrentChange (newPage) {
      this.query.page = newPage
      // 题组列表
      this.randoms()
    },
    // 搜索
    onSubmit () {
      const id = this.tableData.map(item => item.id)
      const res = id.includes(this.query.keyword)
      if (!res) return
      // 获取题组列表
      this.randoms()
    },
    // 组提列表
    async randoms () {
      const res = await randoms(this.query)
      this.tableData = res.data.items
      this.total = res.data.counts
    }
  }
}
</script>

<style scoped>
  .elIcon{
    font-size: 12px;
  }
  .el-card{
    margin: -10px;
  }
  .el-icon-info{
    margin-right: 8px;

  }
  .itemCount{
     margin-bottom: 15px;
     background: #f4f4f5;
     padding:8px 16px;
     font-size: 13px;
     color: #909399;
  }

  .el-input {
    display: inline-block;
    width: 240px;
    font-size: 13px;
  }
  .el-pagination{
    margin-top: 20px;
  }
  .el-pagination.is-background {
      margin-left: -12px;
  }
</style>
